package scau.cn.component.domain;

import java.io.File;

import javax.xml.transform.Source;

import com.sun.xml.internal.bind.v2.runtime.output.DOMOutput;

import scau.cn.component.abstracts.Disk;
import scau.cn.fat.FAT;

public class FileN extends Disk {
	private byte[] content=new byte[1024];

	public FileN(int nameAndLength, short type, byte attribute, FAT fat, Disk parent) {
		this.nameAndLength = nameAndLength;
		this.type = type;
		if(parent != null && parent.getOpType() == 0) {
			//当父母不为空，且父母为只读，那么该目录也只能只读
			this.attribute = 0b00000001;
			System.out.println("FileN:" + this.attribute);
		} else {
			this.attribute = attribute;
		}
		this.fat = fat;
		this.parent = parent;	
		this.start = fat.findFree();
		this.fat.setStatus(start, 255);
	}
	
	public FileN(String name, String type, byte attribute, FAT fat, Disk parent) throws Exception {
		this.nameAndLength = 0;
		this.setName(name);
		this.nameAndLength += 1;
		this.setTypeToDisk(type);
		if(parent != null && parent.getOpType() == 0) {
			//当父母不为空，且父母为只读，那么该目录也只能只读
			this.attribute = 0b00000001;
			System.out.println("FileN:" + this.attribute);
		} else {
			this.attribute = attribute;
		}
		this.start = fat.findFree();
		this.fat = fat;
		this.parent = parent;
		this.fat.setStatus(start, 255);
	}
	@Override
	public boolean modify() {
		// TODO Auto-generated method stub
		return false;
	}

	//好像是对的
	@Override
	public boolean delete() {
		int index = this.start;
		int next =  fat.getStatus(index);
		while(next!=255) {
			fat.setStatus(index, 0);
			index = next;
			next = fat.getStatus(index);
		}
		fat.setStatus(index, 0);
		Catalog parent = (Catalog) this.getParent();
		parent.getComponent().remove(this);
		File file = new File(this.findPath());
		System.out.println(this.findPath() + " " + file.delete());
		return true;
	}

	@Override
	public String findPath() {
		// TODO Auto-generated method stub
		return super.findPath() + ".txt";
	}
}
